package com.geist.soulmates.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.geist.soulmates.domain.User;

@ContextConfiguration(locations = { "classpath:com/geist/soulmates/spring/soulmate-dal-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class ComplexTestUserDao {

	@Autowired
	private UserDao userDao;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {

	}

	@Before
	public void setUp() throws Exception {

	}

	@After
	public void tearDown() throws Exception {

	}

	@Test
	public void userDao_createUser_success() {
		try {
			User user = new User();
			user.setFirstName("Ajay");
			user.setUserName("ajay");
			user.setPassword("password");
			boolean successful = this.userDao.createUser(user);
			assertTrue(successful);
			User fetchedUser = this.userDao.getUser("ajay", "password");
			assertNotNull(fetchedUser);
			assertEquals("Ajay", fetchedUser.getFirstName());
			boolean deleteUser = this.userDao.deleteUser(fetchedUser);
			assertTrue(deleteUser);
		} catch (Exception exception) {
			exception.printStackTrace();
			fail("Exception not expected while saving user.");
		}
	}
}
